import React from 'react';
import { for_obj, is_empty, Info, KDlg, KFind, obj_int, Dlg, ck_empty, obj_str, loc, ck_int, useKForm, KInput } from 'sui';
import { User } from 'cache';
import { get_custom } from './cmd';

const DlgBuy = ({ open, setOpen, hcmd, cur, map_custom }) => {
    const form = useKForm();

    const getCustom = (k) => {
        let sn = "";

        if (cur) {
            sn = obj_str(cur, "custom");
        } else {
            sn = form.get("tech");
        }

        return get_custom(map_custom, sn, k);
    };

    const hok = () => {
        const ck = {
            pnum : v => ck_int(v),
        };

        if (!cur) {
            ck["tech"] = v => ck_empty(v);
        }

        if (form.isErr(ck)) {
            return;
        }

        const cmd = {
            pnum: obj_int(form.value, "pnum"),
            kind: "oil",
            pname: User.uName(),
        };

        let tech = "";

        if (cur) {
            tech = obj_str(cur, "custom");
        } else {
            tech = form.get("tech");
        }

        cmd["tech"] = tech;
        cmd["desc"] = getCustom("unit");
        cmd["techname"] = getCustom("name");

        setOpen(false);
        hcmd(cmd);
    };

    const getCur = () => {
        if (cur) {
            return <div className="text-base mb-2">{loc("l_109")}<span className="ml-8">{obj_str(cur, "name")}</span></div>;
        }

        const lst = [];

        for_obj(map_custom, (k, v) => lst.push(v));

        return <KFind label={loc("l_109")} name="tech" form={form} list={lst}/>;
    };

    const getView = () => {
        let unit = getCustom("unit");
        if (unit) {
            unit = ` ( ${unit} )`
        }

        return <KInput label={`${loc("l_126")}${unit}`} form={form} name="pnum"/>;
    };

    return (
        <Dlg open={open} hcancel={() => setOpen(false)} hcmd={hok} title={loc("l_552")}>
            {getCur()}
            {getView()}
        </Dlg>
    );
};

const DlgVal = (props) => {
    const form = useKForm();

    const ck = {
        snum : v => ck_int(v),
        box : v => ck_int(v),
        num : v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            const n = parseInt(v, 10);
            const dnum = obj_int(props.data, "dnum");
            if (n > dnum) {
                return loc("l_707");
            }

            const snum = obj_int(form.value, "snum");
            const box = obj_int(form.value, "box");
            const count = box * snum;
            if (n > count) {
                return loc("l_707");
            }

            return null;
        }
    };

    const getView = () => {
        const sn = obj_str(props.data, "custom");
        const unit = get_custom(props.map_custom, sn, "unit");
        const box = get_custom(props.map_custom, sn, "box");

        return (
            <>
            <KInput label={`${loc("l_126")} ( ${unit} )`} form={form} name="num"/>
            <KInput label={`${loc("l_130")} ( ${box} )`} form={form} name="box"/>
            <KInput label={`${loc("l_124")} ( ${unit} )`} form={form} name="snum"/>
            </>
        );
    };

    return (
        <KDlg {...props} title={loc("l_741")} ck={ck} form={form}>
            {getView()}
        </KDlg>
    );
};

const DlgNum = (props) => {
    const form = useKForm(props.data);

    const ck = {
        dnum: v => {
            const e = ck_int(v);
            if (e) {
                return e;
            }

            const tmp = parseInt(v, 10);
            const num = obj_int(props.data, "dnum");

            if (tmp > num) {
                return loc("l_707");
            }

            return null;
        },
    };

    const getTitle = () => {
        if (props.fin === "fin") {
            return loc("l_105");
        }

        return loc("l_111");
    };

    const getCol = () => {
        const tmp = props.col;
        if (is_empty(tmp)) {
            return null;
        }

        const ret = [];

        tmp.forEach(d => {
            const t = d.sn;
            if (t === "sn" || t === "dnum") {
                ret.push(d);
            }
        });

        return ret;
    };

    return (
        <KDlg {...props} title={getTitle()} ck={ck} form={form}>
            <Info data={props.data} col={getCol()} hl={["dnum"]}/>
            <KInput label={loc("l_127")} form={form} name="dnum"/>
        </KDlg>
    );
};

export {DlgNum, DlgVal, DlgBuy};
